$("#wizard").steps();

$("#upPwdForm").steps({
    bodyTag: "fieldset",
    labels: {
        cancel: "返回登陆页面",
        finish: "完成",
        next: "下一步",
        previous: "上一步"
    },
    onStepChanging: function (event, currentIndex, newIndex) {
        if (currentIndex > newIndex) {
            return true;
        }
        if (newIndex === 3 && Number($("#age").val()) < 18) {
            return false;
        }
        var form = $(this);
        if (currentIndex < newIndex) {
            $(".body:eq(" + newIndex + ") label.error", form).remove();
            $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
        }
        form.validate().settings.ignore = ":disabled,:hidden";
        return form.valid();
    },
    onStepChanged: function (event, currentIndex, priorIndex) {
        if (currentIndex === 1) {
            getPwdQuestions();
        }
        if (currentIndex === 2 && Number($("#age").val()) >= 18) {
            $(this).steps("next");
        }
        if (currentIndex === 2 && priorIndex === 3) {
            $(this).steps("previous");
        }
    },
    onFinishing: function (event, currentIndex) {
        var form = $(this);
        form.validate().settings.ignore = ":disabled";
        return form.valid();
    },
    onFinished: function (event, currentIndex) {
        pushPwdInfo( $('#newPwd1').val(), $('#pwdQuestions').val(), $('#pwdAnswer').val() );
    },
    onCanceled: function () {
        window.location.href = contextPath + "/login";
    }
}).validate({
    errorPlacement: function (error, element) {
        element.before(error);
    },
    rules: {
        userName: {
            remote: {
                url: contextPath + "/login/userNameCheck",
                type: 'POST',
                data: {
                    userName: function() {
                        return $("#upPwdForm input[name='userName']").val();
                    }
                }
            }
        },
        answer: {
            remote: {
                url: contextPath + '/login/answerCheck',
                type: 'POST',
                data: {
                    answer: function() {
                        return $("#upPwdForm input[name='answer']").val();
                    }
                }
            }
        },
        newPwd: {
            maxlength: 25,
            newPwd: "#required"
        },
        newPwd1: {
            equalTo: "#newPwd"
        },
        pwdQuestions: {
            maxlength: 25
        },
        pwdAnswer: {
            maxlength: 25
        }
    },
    messages: {
        userName: {
            remote: "用户不存在！"
        },
        answer: {
            remote: "回答错误！"
        },
        newPwd: {
            maxlength: "不超过25个字符！"
        },
        newPwd1: {
            equalTo: "密码输入不一致！"
        },
        pwdQuestions: {
            maxlength: "不超过25个字符！"
        },
        pwdAnswer: {
            maxlength: "不超过25个字符！"
        }
    }
});

jQuery.validator.addMethod("newPwd", function (value, element) {
    return this.optional(element) ||
        /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~#\^$@$!%*?&\.])[A-Za-z\d~#$\^@$!%*?&\.]{7,}$/.test(value);
}, "密码必须包含英文字母，数字及特殊字符!");

function getPwdQuestions() {
    $.ajax({
        url : contextPath + '/login/getPwdQuestions',
        type : 'POST',
        success : function (data) {
            $('#parameter').text(data);
        }
    });
}

function pushPwdInfo(pwd, questions, answer) {
    $.ajax({
        url : contextPath + '/login/pushPwdInfo',
        type : 'POST',
        data : {"pwd" : pwd, "questions" : questions, "answer" : answer},
        success : function (data) {
            if (data === true)
                window.location.href = contextPath + "/login";
        }
    });
}
